package com.fjwt.gz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.MchGroupMchEntity;
import com.fjwt.gz.db.dto.PointDTO;
import com.fjwt.gz.db.vo.MchGroupMchVO;
import com.fjwt.gz.db.vo.MchInfoVO;
import com.fjwt.gz.service.mapper.MchGroupMchMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 商圈商户关联表 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class MchGroupMchService extends ServiceImpl<MchGroupMchMapper, MchGroupMchEntity> {

    @Resource
    private MchGroupMchMapper groupMchMapper;

    /**
     * 查询运营端商圈中存在的商户
     * @param page
     * @param groupNo
     * @return
     */
    public IPage<MchGroupMchVO> selectGroupMch(IPage page, String groupNo, String mchNo, String mchName){
        if (StringUtils.isEmpty(groupNo)){
            throw new BizException("请求参数缺失！");
        }
        return groupMchMapper.selectGroupMch(page,groupNo,mchNo,mchName);
    }

    /**
     * 获取商圈中的商家列表
     * @return
     */
    public IPage<MchInfoVO> findMchInfoByGroupId(IPage iPage, String groupNo, PointDTO location, String mchNo, String mchName) {
        if (ObjectUtils.isEmpty(location)){
            throw new BizException("请求参数缺失！");
        }
        IPage<MchInfoVO> page = groupMchMapper.findMchInfoByGroupId(iPage, groupNo, location, mchNo, mchName);
//        Stream<MchInfoVO> sorted = page.getRecords().stream().sorted(Comparator.comparing(MchInfoVO::getOrderSort, Comparator.reverseOrder()));
//        List<MchInfoVO> collect = sorted.collect(Collectors.toList());
//        page.setRecords(collect);
        return page;
    }


    /**
     * 获取商家详情
     * @param mchNo
     * @return
     */
    public MchInfoVO findMchInfoVO(String mchNo) {
        if (StringUtils.isEmpty(mchNo)){
            throw new BizException("请求参数缺失！");
        }
        return groupMchMapper.findMchInfoVO(mchNo);
    }

    /**
     * 删除商圈中的商户
     * @param groupMchId
     */
    public void removeGroupMch(Long groupMchId) {
        if (groupMchId == null){
            throw new BizException("请求参数缺失！");
        }
        removeById(groupMchId);
    }
}
